package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.applozic.mobicomkit.contact.AppContactService;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.SearchRecordUtils;
import com.lottery.ruok.lottrey.adapter.recyclleradapter.BaseAppRecyclerViewAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ResumeChatUserRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.OperationSuccessResponse;
import com.lottery.ruok.lottrey.ui.component.ChannelItemView;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.List;

import utils.Utils;

public class SearchChannelAdapter extends BaseAppRecyclerViewAdapter<GetRoomUsersResponse.ListBean> {

    private AppContactService appContactService;

    private ChannelItemView.OnChannelEventListener onChannelEventListener = new ChannelItemView.OnChannelEventListener() {
        @Override
        public void delete(final GetRoomUsersResponse.ListBean data) {
            Utils.createDialog(mContext, "确定把用户" + getDisplayName(data.getUsername()) + "从搜索记录中移除",
                    "取消", "确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            RetrofitBase.getRetrofit().create(ResumeChatUserRequest.class)
                                    .getResult(UserInfo.getToken(), data.getUsername(), 0)
                                    .compose(RxHelper.<OperationSuccessResponse>handleResult())
                                    .subscribe(new RxSubscribe<OperationSuccessResponse>(mContext) {
                                        @Override
                                        protected void _onNext(OperationSuccessResponse operationSuccessResponse) {
                                            Utils.showToast(operationSuccessResponse.getMsg());
                                            SearchRecordUtils.delete(data.getUsername());
                                            getList().remove(data);
                                            notifyDataSetChanged();
                                        }

                                        @Override
                                        protected boolean showDialog() {
                                            return true;
                                        }
                                    });
                        }
                    });
        }
    };

    public SearchChannelAdapter(Context context) {
        super(context);
        this.appContactService = new AppContactService(mContext);
    }

    public SearchChannelAdapter(Context context, List<GetRoomUsersResponse.ListBean> list) {
        super(context, list);
        this.appContactService = new AppContactService(mContext);

    }

    @Override
    protected View createItemView(Context mContext, int viewType) {
        return new ChannelItemView(mContext);
    }


    @Override
    protected void onBindView(View itemView, int position) {
        if (itemView instanceof ChannelItemView) {
            ChannelItemView view = (ChannelItemView) itemView;
            view.setAppContactService(appContactService);
            view.setData(getList().get(position), null);
            view.setType(ChannelItemView.TYPE_SEARCH);
            view.setOnChannelEventListener(onChannelEventListener);
        }
    }

    private String getDisplayName(String username) {
        return appContactService.getContactById(username).getDisplayName();
    }
}
